﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class query : ModulePage
    {
        protected string strTemp;


        private void O1OOlllO0lO0O11(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.O1OOlllO0lO0O11(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
 

 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.SomeDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.Date1.Value = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-1";
                this.Date2.Value = DateTime.Now.ToString("yyyy-MM-dd");
                ListItem item = new ListItem("所有部门", "");
                this.ddlDeptId.Items.Add(item);
                DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.O1OOlllO0lO0O11(ref this.ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                this.ddlDutyTypeId.DataSource = new AttendConfigDao().SelectAll(base.Conn);
                this.ddlDutyTypeId.DataBind();
                this.ddlDutyTypeId.Items.Insert(0, new ListItem("所有类型", ""));
            }
            if (this.Page.IsPostBack)
            {
                DateTime time9;
                DateTime time10;
                DataTable table6;
                DataTable table7;
                DataTable table8;
                this.pnlMain.Visible = false;
                this.pnlResult.Visible = true;
                DateTime time5 = Convert.ToDateTime(this.Date1.Value);
                this.ViewState["Date1"] = time5.ToString("yyyy-MM-dd");
                DateTime time6 = Convert.ToDateTime(this.Date2.Value);
                this.ViewState["Date2"] = time6.ToString("yyyy-MM-dd");
                this.ViewState["DeptId"] = this.ddlDeptId.SelectedValue;
                this.ViewState["DutyTypeId"] = this.ddlDutyTypeId.SelectedValue;
                TimeSpan span = new TimeSpan(time5.Ticks);
                TimeSpan ts = new TimeSpan(time6.Ticks);
                int num4 = span.Subtract(ts).Duration().Days + 1;
                this.litNavTitle.Text = base.ShowMessage("查询从 " + time5.ToString("D") + " 至 " + time6.ToString("D") + " 共" + num4.ToString() + "天的考勤记录").Replace("180", "330");
                string commandText = "SELECT [Users].[UserId],[Users].[UserName],[Department].[DeptName] FROM [dbo].[Users],[dbo].[Department] WHERE [Users].[DeptId] <> -1 AND [Users].[DeptId] = [Department].[DeptId] ";
                if (this.ddlDeptId.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Users].[DeptId] = " + this.ddlDeptId.SelectedValue;
                }
                if (this.ddlDutyTypeId.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Users].[DutyType] = " + this.ddlDutyTypeId.SelectedValue;
                }
                commandText = commandText + " ORDER BY [Department].[DeptNo] ASC, [Users].[UserNo] ASC";
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                table2.Columns.Add("AllNum", Type.GetType("System.Int32"));
                table2.Columns.Add("LaterNum", Type.GetType("System.Int32"));
                table2.Columns.Add("UpNum", Type.GetType("System.Int32"));
                table2.Columns.Add("BeforeNum", Type.GetType("System.Int32"));
                table2.Columns.Add("DownNum", Type.GetType("System.Int32"));
                int num5 = 30;
                int num6 = 10;
                int num7 = 10;
                int num8 = 60;
                AttendParaDao dao2 = new AttendParaDao();
                dao2.GetPara();
                num5 = dao2.DutyIntervalBefore1;
                num6 = dao2.DutyIntervalAfter1;
                num7 = dao2.DutyIntervalBefore2;
                num8 = dao2.DutyIntervalAfter2;
                if (table2.Rows.Count > 0)
                {
                    for (int j = 0; j < table2.Rows.Count; j++)
                    {
                        int num10;
                        DateTime time8;
                        string userId = table2.Rows[j]["UserId"].ToString();
                        DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, userId);
                        string str3 = (userConfig.Rows[0]["General"] != DBNull.Value) ? userConfig.Rows[0]["General"].ToString() : "";
                        if (str3 != string.Empty)
                        {
                            str3 = "," + str3 + ",";
                        }
                        DataTable table4 = new DataTable();
                        table4.Columns.Add("DutyType", Type.GetType("System.Int32"));
                        table4.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
                        table4.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
                        table4.Columns.Add("EndTime", Type.GetType("System.DateTime"));
                        if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
                        {
                            num10 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                            time8 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                            if (num10 == 1)
                            {
                                time9 = time8.AddMinutes((double)-num5);
                                time10 = time8.AddMinutes((double)num6);
                            }
                            else
                            {
                                time9 = time8.AddMinutes((double)-num7);
                                time10 = time8.AddMinutes((double)num8);
                            }
                            table4.Rows.Add(new object[] { num10, time8, time9, time10 });
                        }
                        if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
                        {
                            num10 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                            time8 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                            if (num10 == 1)
                            {
                                time9 = time8.AddMinutes((double)-num5);
                                time10 = time8.AddMinutes((double)num6);
                            }
                            else
                            {
                                time9 = time8.AddMinutes((double)-num7);
                                time10 = time8.AddMinutes((double)num8);
                            }
                            table4.Rows.Add(new object[] { num10, time8, time9, time10 });
                        }
                        if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
                        {
                            num10 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                            time8 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                            if (num10 == 1)
                            {
                                time9 = time8.AddMinutes((double)-num5);
                                time10 = time8.AddMinutes((double)num6);
                            }
                            else
                            {
                                time9 = time8.AddMinutes((double)-num7);
                                time10 = time8.AddMinutes((double)num8);
                            }
                            table4.Rows.Add(new object[] { num10, time8, time9, time10 });
                        }
                        if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
                        {
                            num10 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                            time8 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                            if (num10 == 1)
                            {
                                time9 = time8.AddMinutes((double)-num5);
                                time10 = time8.AddMinutes((double)num6);
                            }
                            else
                            {
                                time9 = time8.AddMinutes((double)-num7);
                                time10 = time8.AddMinutes((double)num8);
                            }
                            table4.Rows.Add(new object[] { num10, time8, time9, time10 });
                        }
                        if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
                        {
                            num10 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                            time8 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                            if (num10 == 1)
                            {
                                time9 = time8.AddMinutes((double)-num5);
                                time10 = time8.AddMinutes((double)num6);
                            }
                            else
                            {
                                time9 = time8.AddMinutes((double)-num7);
                                time10 = time8.AddMinutes((double)num8);
                            }
                            table4.Rows.Add(new object[] { num10, time8, time9, time10 });
                        }
                        if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
                        {
                            num10 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                            time8 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                            if (num10 == 1)
                            {
                                time9 = time8.AddMinutes((double)-num5);
                                time10 = time8.AddMinutes((double)num6);
                            }
                            else
                            {
                                time9 = time8.AddMinutes((double)-num7);
                                time10 = time8.AddMinutes((double)num8);
                            }
                            table4.Rows.Add(new object[] { num10, time8, time9, time10 });
                        }
                        int num11 = 0;
                        int num12 = 0;
                        int num13 = 0;
                        int num14 = 0;
                        int num15 = 0;
                        for (int k = 0; k < num4; k++)
                        {
                            bool flag;
                            bool flag2;
                            int dayOfWeek = (int)time5.AddDays((double)k).DayOfWeek;
                            if (str3.IndexOf("," + dayOfWeek.ToString() + ",") < 0)
                            {
                                flag = true;
                            }
                            else
                            {
                                flag = false;
                            }
                            commandText = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + "' AND '" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + "' <= [EndDate]";
                            if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, commandText) != null)
                            {
                                flag2 = true;
                                flag = false;
                            }
                            else
                            {
                                flag2 = false;
                            }
                            commandText = "SELECT DutyId,RegisterType,RegisterTime,RegisterFlag,Remark FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + userId + "' AND [RegisterTime] >='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " 23:59:59'";
                            DataTable table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                            for (int m = 0; m < table4.Rows.Count; m++)
                            {
                                DataRow[] rowArray = table5.Select("RegisterType = " + table4.Rows[m]["DutyType"].ToString() + " AND RegisterTime >='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["BeginTime"].ToString()).ToString("HH:mm:ss") + "' AND RegisterTime <='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["EndTime"].ToString()).ToString("HH:mm:ss") + "'");
                                if (rowArray.Length > 0)
                                {
                                    if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 1)
                                    {
                                        flag = false;
                                        num12++;
                                    }
                                    else if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 2)
                                    {
                                        flag = false;
                                        num14++;
                                    }
                                }
                                else if ((str3.IndexOf("," + dayOfWeek.ToString() + ",") < 0) && !flag2)
                                {
                                    commandText = "SELECT [OutId] FROM [dbo].[Attend_Out] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [OutTime1] <='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [OutTime2]>='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                    table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                    if (table6.Rows.Count == 0)
                                    {
                                        commandText = "SELECT [LeaveId] FROM [dbo].[Attend_Leave] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [LeaveDate1] <='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [LeaveDate2]>='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                        table7 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                        if (table7.Rows.Count == 0)
                                        {
                                            commandText = "SELECT [EvectionId] FROM [dbo].[Attend_Evection] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [EvectionDate1] <='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [EvectionDate1]>='" + time5.AddDays((double)k).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table4.Rows[m]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                            table8 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                            if (table8.Rows.Count == 0)
                                            {
                                                if (Convert.ToInt32(table4.Rows[m]["DutyType"].ToString()) == 1)
                                                {
                                                    flag = false;
                                                    num13++;
                                                }
                                                else
                                                {
                                                    flag = false;
                                                    num15++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (flag)
                            {
                                num11++;
                            }
                        }
                        if (num11 != 0)
                        {
                            table2.Rows[j]["AllNum"] = num11;
                        }
                        if (num12 != 0)
                        {
                            table2.Rows[j]["LaterNum"] = num12;
                        }
                        if (num13 != 0)
                        {
                            table2.Rows[j]["UpNum"] = num13;
                        }
                        if (num14 != 0)
                        {
                            table2.Rows[j]["BeforeNum"] = num14;
                        }
                        if (num15 != 0)
                        {
                            table2.Rows[j]["DownNum"] = num15;
                        }
                    }
                }
                this.rptDuty.DataSource = table2;
                this.rptDuty.DataBind();
                time9 = Convert.ToDateTime(time5.ToString("yyyy-MM-dd") + " 00:00:00");
                time10 = Convert.ToDateTime(time6.ToString("yyyy-MM-dd") + " 23:59:59");
                commandText = "SELECT [Attend_Out].[OutId],[Attend_Out].[OutType],[Attend_Out].[OutTime1],[Attend_Out].[OutTime2],[Attend_Out].[Status],A.[UserName] AS [LeaderName],B.[UserName],[Department].[DeptName] FROM [dbo].[Attend_Out],[dbo].[Users] AS A,[dbo].[Users] AS B,[dbo].[Department] WHERE [Attend_Out].[Allow] = 1 AND [Attend_Out].[LeaderId] = A.[UserId] AND [Attend_Out].[UserId] = B.[UserId] AND B.[DeptId] = [Department].[DeptId] AND (([Attend_Out].[OutTime1] >= '" + time9.ToString() + "' AND [Attend_Out].[OutTime1] <= '" + time10.ToString() + "') OR ([Attend_Out].[OutTime2] >= '" + time9.ToString() + "' AND [Attend_Out].[OutTime2] <= '" + time10.ToString() + "')) ORDER BY [Attend_Out].[OutId] ASC";
                table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                if (table6.Rows.Count > 0)
                {
                    this.rptOut.DataSource = table6;
                    this.rptOut.DataBind();
                }
                else
                {
                    this.litOut.Text = base.ShowMessage("无外出记录");
                }
                commandText = "SELECT [Attend_Leave].[LeaveId],[Attend_Leave].[LeaveType],[Attend_Leave].[LeaveDate1],[Attend_Leave].[LeaveDate2],[Attend_Leave].[Status],A.[UserName] AS [LeaderName],B.[UserName],[Department].[DeptName] FROM [dbo].[Attend_Leave],[dbo].[Users] AS A,[dbo].[Users] AS B,[dbo].[Department] WHERE [Attend_Leave].[Allow] IN(1,3) AND [Attend_Leave].[LeaderId] = A.[UserId] AND [Attend_Leave].[UserId] = B.[UserId] AND B.[DeptId] = [Department].[DeptId] AND (([Attend_Leave].[LeaveDate1] >= '" + time9.ToString() + "' AND [Attend_Leave].[LeaveDate1] <= '" + time10.ToString() + "') OR ([Attend_Leave].[LeaveDate2] >= '" + time9.ToString() + "' AND [Attend_Leave].[LeaveDate2] <= '" + time10.ToString() + "')) ORDER BY [Attend_Leave].[LeaveId] ASC";
                table7 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                if (table7.Rows.Count > 0)
                {
                    this.rptLeave.DataSource = table7;
                    this.rptLeave.DataBind();
                }
                else
                {
                    this.litLeave.Text = base.ShowMessage("无请假记录");
                }
                commandText = "SELECT [Attend_Evection].[EvectionId],[Attend_Evection].[EvectionDest],[Attend_Evection].[EvectionDate1],[Attend_Evection].[EvectionDate2],[Attend_Evection].[Status],A.[UserName] AS [LeaderName],B.[UserName],[Department].[DeptName] FROM [dbo].[Attend_Evection],[dbo].[Users] AS A,[dbo].[Users] AS B,[dbo].[Department] WHERE [Attend_Evection].[Allow] = 1 AND [Attend_Evection].[LeaderId] = A.[UserId] AND [Attend_Evection].[UserId] = B.[UserId] AND B.[DeptId] = [Department].[DeptId] AND (([Attend_Evection].[EvectionDate1] >= '" + time9.ToString() + "' AND [Attend_Evection].[EvectionDate1] <= '" + time10.ToString() + "') OR ([Attend_Evection].[EvectionDate2] >= '" + time9.ToString() + "' AND [Attend_Evection].[EvectionDate2] <= '" + time10.ToString() + "')) ORDER BY [Attend_Evection].[EvectionId] ASC";
                table8 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                if (table8.Rows.Count > 0)
                {
                    this.rptEvection.DataSource = table8;
                    this.rptEvection.DataBind();
                }
                else
                {
                    this.litEvection.Text = base.ShowMessage("无出差记录");
                }
            }
        } 

    }
}
